/**
 * @package      JCE Advlink
 * @copyright    Copyright (C) 2008 - 2010 Ryan Demmer. All rights reserved.
 * @author		Ryan Demmer
 * @license      GNU/GPL
 * JCE is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
var JCEWindowPopup = {
	setPopupMode : function(v) {
		$('#window_popup_features').toggle(v);
		$('input:checkbox').attr('disabled', !v);
	},
	
	/**
     * Set proportianl dimension calculations
     * @param {String} w Width
     * @param {String} h Height
     */
    setDimensions : function(w, h) {
        $.Plugin.setDimensions(w, h, 'window_popup_');
    }
};


WFPopups.addPopup('window', {
    /**
     * Initialise the player. Set various player options
     */
    init : function() {
    },

    /**
     * Check if node is a JCE Utiliteis popup
     * @param {Object} node
     */
    check : function(node) {
        var s = node.href;
        
    	return /option=com_jce/.test(s) && /(task|view)=popup/i.test(s);
    },

    /**
     * Get popup parameters
     * @param {Object} n Popup node
     */
    getAttributes: function(n) {
        var ed 		= tinyMCEPopup.editor, data = {};
        var click 	= ed.dom.getAttrib(n, 'onclick');

        var src 	= click.replace(/&amp;/g, '&').replace(/&#39;/g, "'").split("'");
        var params 	= $.String.query(src[1]);
        
        var data = {
        	img			: '',
        	title		: '',
        	print 		: 0,
        	click 		: 0,
        	mode  		: 0,
        	scrollbars 	: /scrollbars=yes/i.test(src),
        	resizable 	: /resizable=yes/i.test(src)
        };
        
        $.each($.extend(data, params), function(k, v) {
        	if (k == 'w') {
        		k = 'width';
        	}
        	
        	if (k == 'h') {
        		k = 'height';
        	}
        	
        	if (k == 'img') {
        		v = ed.documentBaseURI.toRelative(v);
        	}
        	
        	if (k == 'title') {
        		v = v.replace('_', ' ', 'g');
        	}
        	
        	$('#window_popup_' + k).val(v);
        });

        $('#tmp_popup_width').val($('#popup_width').val());
        $('#tmp_popup_height').val($('#popup_height').val());

        $.each(['print', 'click', 'scrollbars', 'resizable'], function(i, v) {
        	$('#window_popup_' + v).attr('disabled', (data.mode == 0));
        });
        
        $('#window_popup_features').toggle((data.mode == 1));
    },

    /**
     * Set Popup Attributes
     * @param {Object} n Link Element
     */
    setAttributes: function(n, args) {
        var ed = tinyMCEPopup.editor;
        
        this.remove(n);
        
        var src 	= ed.dom.getAttrib(n, 'href');
        var title 	= $('#window_popup_title').val();
        
        var features = {
        	print : $('#window_popup_print').is(':checked') ? 1 : 0,
        	click : $('#window_popup_rightclick').is(':checked') ? 1 : 0,
        	mode  : $('#window_popup_mode').val()
        };

        var scrollbars	= $('#window_popup_scrollbars').is(':checked') 	? 'yes' : 'no';
        var resizable	= $('#window_popup_resizable').is(':checked') 	? 'yes' : 'no';
        
        var w = $('#window_popup_width').val();
        var h = $('#window_popup_height').val();

        var args = {
        	img 	: src,
        	title 	: title.replace(' ', '_', 'gi'),
        	width	: w,
        	height	: h
        };       
        
        var props = 'height='+ h +',width='+ w +',top=10,left=10,scrollbars='+ scrollbars +',resizable='+ resizable;
        
        if (features.mode == 1) {
        	$.extend(args, features);
        }
        
        ed.dom.setAttrib(n, 'href', 'index.php?option=com_jce&tmpl=component&view=popup');
        ed.dom.setAttrib(n, 'onclick', "window.open(this.href+'&" + decodeURIComponent($.param(args)) + "','" + title.replace(' ', '_', 'gi') + "','" + props + "');return false;");
    }

});